package org.teachertools.data;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@Entity
public class Competence implements Serializable {

	private static final long serialVersionUID = -5889658578340333068L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Column(nullable = false, length = 100)
	private String nom;

	@ManyToOne(optional = false)
	@JoinColumn(name = "ID_MATIERE")
	private org.teachertools.data.Matiere matiere;

	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
	@JoinColumn(name = "ID_COMPETENCE")
	private Set<Note> notes;

	@ManyToMany(fetch = FetchType.LAZY)// , cascade=CascadeType.REFRESH)
	@JoinTable(
		name = "livret_contenu",
		joinColumns = { @JoinColumn(name="ref_id") },
		inverseJoinColumns = { @JoinColumn(name="id_livret") }
	)
	private List<Livret> livrets;

	public Competence() {
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long pId) {
		this.id = pId;
	}

	public String getNom() {
		return this.nom;
	}

	public void setNom(String pNom) {
		this.nom = pNom;
	}

	public org.teachertools.data.Matiere getMatiere() {
		return this.matiere;
	}

	public void setMatiere(org.teachertools.data.Matiere pMatiere) {
		this.matiere = pMatiere;
	}

	public org.teachertools.data.Categorie getCategorie() {
		if (getMatiere() != null)
			return getMatiere().getCategorie();
		return null;
	}

	public Set<Note> getNotes() {
		return this.notes;
	}

	public void setNotes(Set<Note> pNotes) {
		this.notes = pNotes;
	}

	public List<Livret> getLivrets() {
		return livrets;
	}

	public void setLivrets(List<Livret> pLivrets) {
		livrets = pLivrets;
	}

	@Override
	public int hashCode() {
		HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
		hashCodeBuilder.append(id);
		hashCodeBuilder.append(nom);
		return hashCodeBuilder.toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if ((obj instanceof Competence) == false) {
			return false;
		}
		Competence otherCompetence = (Competence) obj;
		if (otherCompetence == this) {
			return true;
		}
		EqualsBuilder equalsBuilder = new EqualsBuilder();
		equalsBuilder.append(id, otherCompetence.getId());
		return equalsBuilder.isEquals();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
			.append("id", getId())
			.append("nom", getNom())
			.toString();
	}

}
